package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;

class BankService {


    public static void addClient(Bank bank, Client client) throws BankException {

        int length = bank.clients.length;

        for (int i = 0; i < length; i++) {

            if (bank.clients[i] != null && bank.clients[i].fio == client.fio) {
                throw new ClientExistsException("ClientExistsException\n");
            }

            if (bank.clients[i] == null) {
                bank.clients[i] = client;
                for (int k = 0; k < bank.listener.length; k++) {
                    bank.listener[k].onClientAdded(client);
                }
                return;
            }
        }
        throw new BankException("BankException\n");
    }


    public static void printMaximumAmountToWithdraw(Bank bank) {

        int length = bank.clients.length;

        for (int i = 0; i < length; i++) {
            if (bank.clients[i] == null) {
                break;
            }

            System.out.printf("Client %s \n", bank.clients[i].getClientSalutation());

            if (bank.clients[i].savingAccount != null) {
                System.out.printf("balance: %.2f\n", bank.clients[i].savingAccount.getBalance());
            }
            if (bank.clients[i].checkingAccount != null) {
                System.out.printf("balance with overdraft: %.2f\n", bank.clients[i].checkingAccount.maximumAmountToWithdraw());
            }


        }
    }

}